package com.jxtc.enterprise.delivery.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.common.vo.PaginationResponseVO;
import com.jxtc.enterprise.delivery.service.RiderTodayTaskService;
import com.jxtc.enterprise.delivery.vo.todayTask.RiderTodayTaskVO;
import com.jxtc.enterprise.delivery.vo.todayTask.StoreOrderDetailVO;
import com.jxtc.enterprise.delivery.vo.todayTask.TodayTaskBriefVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 配送员 H5 "今日任务" 页面相关接口
 */
@RestController
@RequestMapping("/jxtc/enterprise/delivery/todayTask")
@RequiredArgsConstructor
@Slf4j
public class RiderTodayTaskController {

    private final RiderTodayTaskService riderTodayTaskService;

    /**
     * 接口 6.2.1 根据任务状态获取任务列表
     *
     * @param riderId 骑手 ID
     * @param status  任务状态
     *                0：全部，
     *                1：未送达，
     *                2：已送达
     * @return 分页后的今日任务列表
     */
    @GetMapping("/queryTaskByStatus")
    public Result<PaginationResponseVO<RiderTodayTaskVO>> queryTaskByStatus(@RequestParam(value = "riderId") String riderId,
                                                                            @RequestParam(value = "status", required = false, defaultValue = "0") Integer status,
                                                                            @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                            @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        log.info("/queryTaskByStatus, riderId: {}, status: {}, pageNo: {}, pageSize: {}", riderId, status, pageNo, pageSize);
        PaginationResponseVO<RiderTodayTaskVO> vo = riderTodayTaskService.queryTaskByStatus(riderId, status, pageNo, pageSize);
        log.info("/queryTaskByStatus, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 接口 6.2.2 查看某个配送任务中从某个店铺取餐的订单详情
     *
     * @param taskId  配送任务 ID
     * @param storeId 店铺 ID
     * @return 订单详情
     */
    @GetMapping("/queryOrdersDetailByTaskIdStoreId")
    public Result<StoreOrderDetailVO> queryOrdersDetailByTaskIdStoreId(@RequestParam("taskId") String taskId, @RequestParam("storeId") String storeId) {
        log.info("/queryOrdersDetailByTaskIdStoreId, taskId: {}, storeId: {}", taskId, storeId);
        StoreOrderDetailVO vo = riderTodayTaskService.queryOrdersDetailByTaskIdStoreId(taskId, storeId);
        log.info("/queryOrdersDetailByTaskIdStoreId, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }

    /**
     * 接口 6.2.3 确认已经从某个店铺取餐
     *
     * @param taskId  配送任务 ID
     * @param storeId 店铺 ID
     * @return code 为 200 时表示确认成功；code 为 400 时表示确认失败，message 字段给出提示消息
     */
    @GetMapping("/confirmOrdersFetched")
    public Result<Void> confirmOrdersFetched(@RequestParam("taskId") String taskId, @RequestParam("storeId") String storeId) {
        log.info("/confirmOrdersFetched, taskId: {}, storeId: {}", taskId, storeId);
        riderTodayTaskService.confirmOrdersFetched(taskId, storeId);
        log.info("/confirmOrdersFetched, succeed");
        return ResultGenerator.genSuccessResult();
    }


    /**
     * 接口 6.2.4 确认已经送达到公司
     *
     * @param taskId 配送任务 ID
     * @return code 为 200 时表示确认成功；code 为 400 时表示确认失败，message 字段给出提示消息
     */
    @GetMapping("/confirmOrdersDelivered")
    public Result<Void> confirmOrdersDelivered(@RequestParam("taskId") String taskId) {
        log.info("/confirmOrdersDelivered, taskId: {}", taskId);
        riderTodayTaskService.confirmOrdersDelivered(taskId);
        log.info("/confirmOrdersDelivered, succeed");
        return ResultGenerator.genSuccessResult();
    }


    /**
     * 接口 6.2.5 获取今日任务数及未完成数量
     *
     * @param riderId 配送员 ID
     * @return 今日任务数及未完成数量
     */
    @GetMapping("/queryTodayTaskBrief")
    public Result<TodayTaskBriefVO> queryTodayTaskBrief(@RequestParam("riderId") String riderId) {
        log.info("/queryTodayTaskBrief, riderId: {}", riderId);
        TodayTaskBriefVO vo = riderTodayTaskService.queryTodayTaskBrief(riderId);
        log.info("/queryTodayTaskBrief, response: {}", JSONObject.toJSONString(vo));
        return ResultGenerator.genSuccessResult(vo);
    }
}
